package com.atguigu.schedule.service.Impl;

import com.atguigu.schedule.DAO.SysUserDAO;
import com.atguigu.schedule.DAO.impl.SysUserDAOImpl;
import com.atguigu.schedule.pojo.SysUser;
import com.atguigu.schedule.service.SysUserService;
import com.atguigu.schedule.utils.JDBCUtils;
import com.atguigu.schedule.utils.MD5Utils;

import java.sql.Connection;

public class SysUserServiceImpl implements SysUserService {
    @Override
    public int register(SysUser sysUser) {
        SysUserDAO userDAO = new SysUserDAOImpl();
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            long count = userDAO.getUserCountWithName(conn, sysUser.getUserName());
            if (count > 0) {
                // 重复注册错误
                return 101;
            }
            sysUser.setUserPwd(MD5Utils.encrypt(sysUser.getUserPwd()));
            int rowCount = userDAO.insert(conn, sysUser);
            if (rowCount > 0) {
                // 无错误
                return 0;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.closeResource(conn, null, null);
        }
        // 注册失败错误
        return 100;
    }
}
